﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;

namespace DAL
{
    public class ArticlesDAL
    {
        //查询所有帖子
        public List<Articles> GetAll()
        {
            using (TuCaoWangEntities db = new TuCaoWangEntities())
            {
                //return db.Articles.ToList();
                return db.Articles.Include("Users").Include("Pingluns").ToList();//贪婪加载
            }
        }

        //按条件查询
        public List<Articles> GetAllByTiao(string stxt)
        {
            using (TuCaoWangEntities db = new TuCaoWangEntities())
            {
                return db.Articles.Include("Users").Include("Pingluns")
                       .Where(a => a.Content.Contains(stxt)).ToList();//贪婪加载
            }
        }

        //查询帖子详情
        public Articles GetById(int id)
        {
            using (TuCaoWangEntities db = new TuCaoWangEntities())
            {
                Articles a = db.Articles.Find(id);
                db.Entry(a).Reference("Users").Load();//显示加载实体对象
                return a;
            }
        }

        //查询帖子评论
        public List<Pingluns> GetPinglunsById(int id)
        {
            using (TuCaoWangEntities db = new TuCaoWangEntities())
            {
                //贪婪加载
                return db.Pingluns.Include("Users")
                    .Where(p => p.ArticleId == id)
                    .OrderByDescending(p => p.CreateTime).ToList();
            }
        }

        //点赞
        public bool Zan(int id)
        {
            using (TuCaoWangEntities db = new TuCaoWangEntities())
            {
                Articles a = db.Articles.Find(id);
                a.Zan += 1;
                db.SaveChanges();
                return true;
            }
        }
        //踩
        public bool Cai(int id)
        {
            using (TuCaoWangEntities db = new TuCaoWangEntities())
            {
                Articles a = db.Articles.Find(id);
                a.Cai += 1;
                db.SaveChanges();
                return true;
            }
        }
        //浏览
        public bool Dian(int id)
        {
            using (TuCaoWangEntities db = new TuCaoWangEntities())
            {
                Articles a = db.Articles.Find(id);
                a.Dian += 1;
                db.SaveChanges();
                return true;
            }
        }

        //发帖
        public bool Add(Articles a)
        {
            try
            {
                using (TuCaoWangEntities db = new TuCaoWangEntities())
                {
                    db.Articles.Add(a);//添加到集合
                    db.SaveChanges();//保存到数据库
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        //发表评论
        public bool AddPinglun(Pingluns p)
        {
            try
            {
                using (TuCaoWangEntities db = new TuCaoWangEntities())
                {
                    db.Pingluns.Add(p);//添加到集合
                    db.SaveChanges();//保存到数据库
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        //按用户查询帖子
        public List<Articles> GetArticlesByUser(int id)
        {
            using (TuCaoWangEntities db = new TuCaoWangEntities())
            {
                Users u = db.Users.Find(id);
                return u.Articles.ToList();//延迟加载
            }
        }

        //删除
        public bool Del(int id)
        {
            try
            {
                using (TuCaoWangEntities db=new TuCaoWangEntities())
                {
                    Articles a = db.Articles.Find(id);//获取要删除的帖子对象
                    db.Articles.Remove(a);//从集合移除对象
                    db.SaveChanges();//保存到数据库
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        //修改
        public bool Update(Articles update)
        {
            try
            {
                using (TuCaoWangEntities db = new TuCaoWangEntities())
                {
                    Articles old = db.Articles.Find(update.ID);//获取要修改的帖子对象
                    old.Content=update.Content;//修改对象属性:帖子内容
                    db.SaveChanges();//保存到数据库
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
